package database_interaction;

/**
 * @file UpdateQuery.java
 * @version 0.1
 * @author James Dibble 10009689
 */
public class UpdateQuery extends SQLQuery{
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public UpdateQuery(String table) {
        super(QueryType.UPDATE, table);
    }
    
    public UpdateQuery(String table, String filter) throws JDBCException {
        super(QueryType.UPDATE, table);
        super.setFilter(filter);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    @Override
    public String getQueryString() throws JDBCException {
        if(super._parms.getColumns().size() != super._parms.getColumns().size())
            throw new JDBCException("The number of columns does not match the number of parameters.");
        if(super.getFilter() == null || "".equals(super.getFilter()))
            throw new JDBCException("An update query MUST have a filter.");
        String strValues = "";
        int intCount = 0;
        for(Parameter thisParm: super._parms.getParms()){
            strValues += super._parms.getColumns().get(intCount) + "=";
            if(thisParm instanceof NVarChar){
                strValues += "'" + thisParm.value() + "',";
            }
            else{
                strValues += thisParm.value() + ",";
            }
            intCount++;
        }
        strValues = strValues.substring(0, strValues.length() - 1);
        
        return String.format("UPDATE %s SET %s WHERE %s", 
                             super.getTable(),
                             strValues,
                             super.getFilter());
    }
    // </editor-fold>
}